from django.http.response import JsonResponse
from django.shortcuts import render, redirect, HttpResponse
from web import models
from web.Forms import project
from utils.tencent.cos import create_bucket
import time

'''
项目view
'''


def project_list(request):
    """
    创建项目
    """
    dic = {
        'status': 1,
        'msg': ''
    }
    if request.method == 'GET':
        form = project.project_form(request)
        # 我创建的 我参加的 星标
        # 获取所有的项目，判断是否星标
        project_dict = {'my_project': [], 'star': [], 'join_project': []}
        # 我创建的所有项目包含星标
        project_obj = models.Project.objects.filter(creator=request.tracer.user)
        for row in project_obj:
            if row.star:
                project_dict['star'].append({'value': row, 'type': 'my'})
            else:
                project_dict['my_project'].append({'value': row, 'type': 'my'})
        # 我参加的所有项目包含星标
        join_project_obj = models.ProjectUser.objects.filter(user=request.tracer.user)
        for item in join_project_obj:
            if item.star:
                project_dict['star'].append({'value': item.project, 'type': 'join'})
            else:
                project_dict['join_project'].append({'value': item.project, 'type': 'join'})

        return render(request, 'project_list.html', locals())
    form = project.project_form(request, data=request.POST)
    if form.is_valid():
        # 校验完成保存数据
        # creator 需要手动传参，前端没有传，form里面虽然没有展示完全但是model导入了

        # 传值 手机号-当前时间-APPID
        bucket = '{}-{}-1305448189'.format(request.tracer.user.mobile_phone, str(int(time.time())))
        region = 'ap-chengdu'
        # 1.创建bucket
        create_bucket(Bucket=bucket, region=region)
        # 2. 创建项目
        form.instance.creator = request.tracer.user
        form.instance.bucket = bucket
        form.instance.region = region
        instance = form.save()
        # 项目初始化问题类型
        issues_type_object_list = []
        for item in models.IssuesType.PROJECT_INIT_LIST:
            issues_type_object_list.append(models.IssuesType(project=instance, title=item))
        models.IssuesType.objects.bulk_create(issues_type_object_list)

        return JsonResponse(dic)
    dic['status'] = 0
    dic['msg'] = form.errors
    return JsonResponse(dic)


# 星标
def project_star(request, project_type, project_id):
    """添加星标"""
    if project_type == 'my':
        models.Project.objects.filter(creator=request.tracer.user, id=project_id).update(star=True)
    elif project_type == 'join':
        models.ProjectUser.objects.filter(creator=request.tracer.user, id=project_id).update(star=True)
    else:
        return redirect('project_list')
    return redirect('project_list')


# 删除星标
def project_ustar(request, project_type, project_id):
    """删除星标"""
    if project_type == 'my':
        models.Project.objects.filter(creator=request.tracer.user, id=project_id).update(star=False)
    elif project_type == 'join':
        models.ProjectUser.objects.filter(creator=request.tracer.user, id=project_id).update(star=False)
    else:
        return redirect('project_list')
    return redirect('project_list')
